<template>
	<view>
		<image src="/static/work1.jpg" mode="" class="img_bg"></image>
		<view class="" style="position: absolute; top: 0; width: 100%;">

			<view class="margin-t60 margin-r30 margin-l30 card">
				<view class="border-bottom bg_white">
					<view class=" display justify-between align-center">
						<view class="text-36 padding-t30 padding-b30">确认风险</view>
						<radio-group @change="radioChange">
							<view class="display justify-between align-center padding-r30">
								<block v-for="(item,index) in myRadio" :key="index">
									<view class="display justify-between align-center padding-l90">
										<radio style="transform:scale(0.7);" :value="item.name" :checked="index === current" />
										<view :style="item.name=='存在'?'color:#FF5444':'color:#00C07F'" style="display: inline-block;">{{item.name}}</view>
									</view>
								</block>
							</view>
						</radio-group>
					</view>
				</view>
				<view class="" v-show="current1==2">


					<view class="border-bottom display justify-between align-center">
						<view class="text-36 padding-t30 padding-b30">计划整改时间</view>
						<view class="display align-center">
							<view>
								<uni-calendar ref="calendar" :insert="false" @confirm="confirm" />
								<view @click="open">{{checkData?checkData:'请选择整改时间'}}</view>
							</view>
						</view>
					</view>
					<view class="border-bottom display justify-between align-center">
						<view class="text-36 padding-t30 padding-b30">详细时间</view>
						<view class="display align-center">
							<view>
								<picker mode="time" :value="checkTime" start="09:01" end="21:01" @change="bindTimeChange">
									<view class="">{{checkTime?checkTime:'请选择详细时间'}}</view>
								</picker>
								
							</view>
						</view>
					</view>
					<view class="border-bottom display justify-between align-center" style="height: 98rpx;">
						<input type="text" value="" placeholder="搜索整改人" v-model="serch_user"/>
						<view class="all_serchBtn" @click="serchUser()">
							搜索
						</view>
					</view>
					
					<view class="" style="width: 400rpx; position: fixed;z-index: 999;background: #FFFFFF;box-shadow: 1px 3px 15px 0px rgba(3, 70, 126, 0.2);" v-show="show_users">
						<scroll-view scroll-y="true" style="height: 350rpx;">
							<view class="" v-for="item in user_list" @click="setUser(item)" style="width: 100%; height: 70rpx;border-bottom: 1rpx solid #CBCBCB;display: flex;justify-content: center;align-items: center;">
								<text>{{item.nickname}}</text>
							</view>
						</scroll-view>
					</view>
					<view class="border-bottom display justify-between align-center">
						<view class="text-36 padding-t30 padding-b30">责任部门</view>
						<view class="display align-center">
							<picker @change="bindPickerChange" :value="index" custom-item="" :range="array" :range-key="'position_name'">
								<view class="">
									{{!show?'请选择整改部门':bumen_name}}
								</view>
							</picker>
						</view>
					</view>
					<view class="border-bottom display justify-between align-center">
						<view class="text-36 padding-t30 padding-b30">责任人</view>
						<view class="display align-center">
							<picker @change="bindPickerChange2" :value="index1" custom-item="" :range="array1" :range-key="'nickname'">
								<view class="">
									{{!show1?'请选择整改人':array1[index1].nickname}}
								</view>
							</picker>
						</view>
					</view>
					<view class="border-bottom display justify-between align-center">
						<view class="text-36 padding-t30 padding-b30">风险分类</view>
						<view class="display align-center">
							<picker @change="bindPickerChange3" :value="index2" custom-item="" :range="array2" :range-key="'safety_name'">
								<!-- <view class="text-28 padding-t30 padding-b30" v-show='show1'>请选择整改人</view> -->
								<view class="">
									{{!show2?'请选择风险分类':array2[index2].safety_name}}
								</view>
							</picker>
						</view>
					</view>
				</view>
				<view class="">
					<button @click="sign()" class="loginButton margin-t120" :disabled="disable_check">提交</button>
				</view>
			</view>
		</view>
	</view>
</template>
<script>
	import uniCalendar from '@/components/uni-calendar/uni-calendar.vue'
	export default {
		components: {
			uniCalendar
		},
		data() {
			return {
				show_users:false,
				serch_user:'',
				user_list:[],
				index: [0],
				index1: [0],
				index2: [],
				myRadio: [{
						name: '存在',
						id: 1
					},
					{
						name: '不存在',
						id: 2
					}
				],
				uid: '',
				token: '',
				array: [{
					"position_name": '请选择整改部门'
				}],
				array1: [{
					"nickname": '请选择整改人'
				}],
				array2: [{
					"safety_name": '选择风险分类'
				}],
				position_rand: 0,
				rectify_uid: '',
				safety_id: '',
				nickname1: '',
				show: true,
				show1: true,
				show2: false,
				checkData: '',
				checkTime: '',
				disable_check: false,
				id: '', //巡检记录id
				current1: 2,
				bumen_name:''
			}
		},
		onLoad: function(options) {
			this.show1 = false
			this.id = options.id
			console.log(this.id)
			this.uid = uni.getStorageSync('user').uid;
			this.token = uni.getStorageSync('user').token;
			this.rectify_uid = uni.getStorageSync('user').company_id;
			this.position_rand = uni.getStorageSync('user').company_id;
			this.nickname1 = uni.getStorageSync('user').company_name;
			console.log(this.nickname1)
			console.log(this.position_rand)
			this.getUserPositon();
			this.getDangerType()
			this.getBumen();
		},
		methods: {
			setUser(item){
				console.log('dianwole')
				// this.show = true
				this.show1 = true
				this.show_users = false
				this.rectify_uid = item.id
				// this.index1 = 0
				this.array1[this.index1].nickname = item.nickname
				console.log(item)
				for(let i =0; i< this.array.length;i++){
					if(item.position_id == this.array[i].id){
						this.bumen_name =  this.array[i].position_name
					}
				}
				this.show = true
			},
			
			serchUser(){
				let that = this
				
				uni.request({
					url: 'https://aqmore.sxpz.vip/AppApi/User/users?uid=' + this.uid + '&token=' + this.token + '&nickname=' +
						this.serch_user,
					method: 'GET',
					header: {
						'content-type': 'application/x-www-form-urlencoded'
					},
					success: function(res) {
						that.user_list = res.data.datas
						if(that.user_list.length === 0){
							that.show_users = false
							uni.showToast({
								title:'未找到查询人',
								icon:'none'
							})
							return
						}
						that.show_users = true
						console.log(that.user_list)
					}
				})
			},
			
			
			getDangerType() {
				var that = this
				uni.request({
					url: 'https://aqmore.sxpz.vip/AppApi/Danger/getDangerType?uid=' + this.uid,
					method: 'GET',
					header: {
						'content-type': 'application/x-www-form-urlencoded'
					},
					success: function(res) {
						console.log(res)
						that.array2 = res.data.datas
						console.log(that.array2)
						// that.array.unshift('');
						// that.array[0].position_name='111'
						// that.array[0] = that.array[0].concat({position_name: 'waadawd'})
						// console.log(that.array[0])
					}
				})
			},
			open() {
				this.$refs.calendar.open();
			},
			getBumen(){
				var that = this
				uni.request({
					url: 'https://aqmore.sxpz.vip/AppApi/User/position?uid=' + this.uid + '&token=' + this.token,
					method: 'GET',
					header: {
						'content-type': 'application/x-www-form-urlencoded'
					},
					success: function(res) {
						console.log(res)
						that.array = res.data.datas
					}
				})
				this.show = false
			},
			
			confirm(e) {
				console.log(this.checkData)
				console.log(e);
				this.checkData = e.fulldate
				var that = this
				uni.request({
					url: 'https://aqmore.sxpz.vip/AppApi/User/position?uid=' + this.uid + '&token=' + this.token,
					method: 'GET',
					header: {
						'content-type': 'application/x-www-form-urlencoded'
					},
					success: function(res) {
						console.log(res)
						that.array = res.data.datas
					}
				})
				this.show = false
				this.array1[0].nickname == '请选择整改人'
				this.show1 = ''
				console.log(this.array1)
			},
			radioChange(e) {
				console.log(e)
				this.radiochange = e.detail.value;
				this.current1 = e.detail.value == '不存在' ? '1' : '2'
				console.log(this.current1)

			},
			getUserPositon() {


			},
			bindTimeChange: function(e) {
				this.checkTime = e.target.value
			},
			bindPickerChange: function(e) {
				this.show = true
				this.show1 = false
				var that = this
				console.log('picker1发送选择改变，携带值为', e.target.value)
				this.index = e.target.value
				this.position_rand = this.array[this.index].id
				console.log(this.position_rand)
				this.bumen_name =  this.array[this.index].position_name
				uni.request({
					url: 'https://aqmore.sxpz.vip/AppApi/User/users?uid=' + this.uid + '&token=' + this.token + '&position_id=' +
						this.position_rand,
					method: 'GET',
					header: {
						'content-type': 'application/x-www-form-urlencoded'
					},
					success: function(res) {
						console.log(res)
						that.array1 = res.data.datas
					}
				})

			},
			bindPickerChange2: function(e) {
				this.show1 = true
				console.log('picker1发送选择改变，携带值为', e.target.value)
				this.index1 = e.target.value
				this.rectify_uid = this.array1[this.index1].id
				console.log(this.rectify_uid)
			},
			bindPickerChange3: function(e) {
				this.show2 = true
				console.log('picker1发送选择改变，携带值为', e.target.value)
				this.index2 = e.target.value
				this.safety_id = this.array2[this.index2].id
				console.log(this.safety_id)
			},
			sign() {
				this.disable_check = true
				var that = this
				if(this.radiochange != '存在'&&this.radiochange != '不存在'){
					uni.showToast({
						title: '请确认风险点状态',
						icon: 'none'
					})
					this.disable_check = false
					return false
				}
				if (this.radiochange == '存在') {
					if (this.checkData == '') {
						uni.showToast({
							title: '请选择整改时间',
							icon: 'none'
						})
						this.disable_check = false
						return false
					}
					/* if(this.show==false){
						uni.showToast({
							title: '请选择整改部门',
							icon: 'none'
						})
						this.disable_check = false
						return false
					} */
					if (this.array1 == '' || this.array1[0].nickname == '请选择整改人') {
						uni.showToast({
							title: '请选择整改人',
							icon: 'none'
						})
						this.disable_check = false
						return false
					}
					if (this.show1 == false) {
						uni.showToast({
							title: '请选择整改人',
							icon: 'none'
						})
						this.disable_check = false
						return false
					}
					if (this.show2 == false) {
						uni.showToast({
							title: '请选择隐患分类',
							icon: 'none'
						})
						this.disable_check = false
						return false
					}
					var overtime = this.checkData+' '+ this.checkTime
					console.log(overtime)
					uni.request({
						url: 'https://aqmore.sxpz.vip/AppApi/Convenient/setAssign?uid=' + this.uid + '&token=' + this.token +
							'&id=' + this.id + '&rectify_overtime=' + overtime + '&rectify_uid=' + this.rectify_uid+ '&danger_type=' +this.safety_id,
						method: 'GET',
						header: {
							'content-type': 'application/x-www-form-urlencoded'
						},
						success: function(res) {
							that.disable_check = true
							console.log(res)
							uni.showToast({
								title: res.data.datas.msg,
								icon: 'success'
							})
							setTimeout(() => {
								uni.switchTab({
									url: '../../workspace'
								})
							}, 1500)
						}
					})
				}
				if (this.radiochange == '不存在') {
					uni.request({
						url: 'https://aqmore.sxpz.vip/AppApi/Convenient/setAssign?uid=' + this.uid + '&token=' + this.token +
							'&id=' + this.id + '&status=' + this.current1,
						method: 'GET',
						header: {
							'content-type': 'application/x-www-form-urlencoded'
						},
						success: function(res) {
							that.disable_check = true
							console.log(res)
							uni.showToast({
								title: res.data.datas.msg,
								icon: 'success'
							})
							setTimeout(() => {
								uni.switchTab({
									url: '../../workspace'
								})
							}, 1500)
						}
					})
				}
			}
		}
	}
</script>

<style>
	page {
		width: 100%;
		height: 100%;
		/* background-color: #0d98ed; */
	}

	.loginButton {
		height: 98upx;
		line-height: 98upx;
		background: #65caff;
		box-shadow: 0 2upx 10upx 0 rgba(115, 241, 255, 0.8);
		border-radius: 49upx;
		color: #fff;
		text-align: center;

		position: relative;
	}
</style>
